package cn.innosmart.aq.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.daimajia.androidanimations.library.Techniques;
import com.daimajia.androidanimations.library.YoYo;
import com.tutk.p2p.ConnectionEntity;
import com.tutk.p2p.ConnectionManager;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import cn.innosmart.aq.R;
import cn.innosmart.aq.bean.AquariumBean;
import cn.innosmart.aq.manager.PlaceManager;
import cn.innosmart.aq.util.SystemConstant;
import cn.innosmart.aq.view.activity.base.BaseActivity;

/**
 * Created by David on 2015/7/23.
 */
public class PlaceModifyActivity extends BaseActivity {
    private EditText etAqname;
    private ImageView ivChoosedevice;
    private ConnectionEntity connectionEntity;

    private void assignViews() {
        etAqname = (EditText) findViewById(R.id.et_aqname);
        ivChoosedevice = (ImageView) findViewById(R.id.iv_choosedevice);
    }

    private void initListener() {
        ivChoosedevice.setOnClickListener(mOnClickListener);
    }

    private View.OnClickListener mOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.et_aqname:
                    showToast("功能正在开发！");
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_placemodify);
        assignViews();
        initListener();
        setBar();
        getData();
    }

    private void getData() {
        Intent intent = getIntent();
        String UID = intent.getStringExtra("UID");
        connectionEntity = ConnectionManager.getInstance().getConnectionEntityFromUID(UID);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        getMenuInflater().inflate(R.menu.menu_toolbar_modify_place, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    protected void onResume() {
        setBaseDialogContext(this);
        super.onResume();
        toolbar.setNavigationIcon(null);
    }

    /**
     * 设置界面Toolbar
     */
    private Toolbar toolbar;

    private void setBar() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        ((ImageView) findViewById(R.id.iv_headicon)).setImageResource(R.drawable.toolbar_back);
        toolbar.setTitle("");
        ((TextView) findViewById(R.id.tv_toolbar)).setText(getString(R.string.activity_placemodify_modify));
        setSupportActionBar(toolbar);
        toolbar.setOnMenuItemClickListener(onMenuItemClick);
        findViewById(R.id.iv_headicon).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                overridePendingTransition(R.anim.abc_fade_in, R.anim.abc_fade_out);
            }
        });
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }


    private Toolbar.OnMenuItemClickListener onMenuItemClick = new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            switch (menuItem.getItemId()) {
                case R.id.action_sure_aquarium:
                    showLoadingDialog(PlaceModifyActivity.this, getString(R.string.dialog_progress_loading));
                    addPlace();
                    break;
            }
            return true;
        }
    };

    //设置Drawer
    private DrawerLayout mDrawer;

    private void addPlace() {
        String name = etAqname.getText().toString().trim();
        if (checkAquariumName(name)) {
            aquariumBean = new AquariumBean();
            JSONObject jParams = new JSONObject();
            try {
                jParams.put("title", name);
                jParams.put("icon_type", "icon_type_2");
                jParams.put("icon_data", "icon_data_2");
                jParams.put("order", 1);
                JSONArray jAddr = new JSONArray();
                jParams.put("addresses", jAddr);
                aquariumBean.setTitle(name);
                aquariumBean.setIcon_type("icon_type_2");
                aquariumBean.setIcon_data("icon_data_2");
                aquariumBean.setOrder(1);
                aquariumBean.setDeivices(new ArrayList<String>());
            } catch (JSONException e) {
                e.printStackTrace();
            }
            PlaceManager.getInstance().Add(connectionEntity, jParams.toString(), new PlaceManager.AddCallBack() {
                @Override
                public void onAddResponseCallBack(int ret, String uid) {
                    if (ret == 0) {
                        aquariumBean.setId(uid);
                        hideLoadingDialog();
                        SystemConstant.aquariumList.add(aquariumBean);
                        finish();
                        overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
                    }
                }
            });

        } else {
            hideLoadingDialog();
        }
    }

    private AquariumBean aquariumBean;

    private boolean checkAquariumName(String name) {
        if (name.equals("")) {
            showToast(getString(R.string.activity_placemodify_error_none_name));
            YoYo.with(Techniques.Shake).playOn(findViewById(R.id.et_aqname));
            return false;
        }
        if (AquariumBean.checkAquariumNameExist(name)) {
            showToast(getString(R.string.activity_placemodify_error_exist_name));
            YoYo.with(Techniques.Shake).playOn(findViewById(R.id.et_aqname));
            return false;
        }
        return true;

    }

}
